package org.example.system.controller.system;

import lombok.RequiredArgsConstructor;
import org.example.common.core.constants.TenantConstants;
import org.example.common.core.domain.R;
import org.example.common.core.utils.page.Pager;
import org.example.common.core.validate.AddGroup;
import org.example.common.core.validate.EditGroup;
import org.example.common.excel.utils.ExcelUtil;
import org.example.common.log.annotation.OperatorLog;
import org.example.common.log.enums.BusinessType;
import org.example.common.security.annotation.PreAuthorize;
import org.example.common.web.annotation.RepeatSubmit;
import org.example.system.domain.bo.SysTenantPackageBo;
import org.example.system.domain.vo.SysTenantPackageVo;
import org.example.system.service.ISysTenantPackageService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

import static org.example.common.core.domain.R.toAjax;

/**
 * 租户套餐管理
 *
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/tenant/package")
public class SysTenantPackageController   {

    private final ISysTenantPackageService tenantPackageService;

    /**
     * 查询租户套餐列表
     */
    @PreAuthorize(hasAnyPermi = "system:tenantPackage:list",andRoles = TenantConstants.SUPER_ADMIN_ROLE_KEY)
    @PostMapping("/list")
    public Pager<SysTenantPackageVo> list(@RequestBody Pager pageQuery) {
        return tenantPackageService.queryPageList(pageQuery);
    }

    /**
     * 查询租户套餐下拉选列表
     */
    @PreAuthorize(hasAnyPermi = "system:tenantPackage:list",andRoles = TenantConstants.SUPER_ADMIN_ROLE_KEY)
    @GetMapping("/selectList")
    public R<List<SysTenantPackageVo>> selectList() {
        return R.ok(tenantPackageService.selectList());
    }

    /**
     * 导出租户套餐列表
     */
    @PreAuthorize(hasAnyPermi = "system:tenantPackage:export",andRoles = TenantConstants.SUPER_ADMIN_ROLE_KEY)
    @OperatorLog(title = "租户套餐", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@RequestBody Pager pageQuery, HttpServletResponse response) {
        List<SysTenantPackageVo> list = tenantPackageService.queryList(pageQuery);
        ExcelUtil.exportExcel(list, "租户套餐", SysTenantPackageVo.class, response);
    }

    /**
     * 获取租户套餐详细信息
     *
     * @param packageId 主键
     */
    @PreAuthorize(hasAnyPermi = "system:tenantPackage:query",andRoles = TenantConstants.SUPER_ADMIN_ROLE_KEY)
    @GetMapping("/{packageId}")
    public R<SysTenantPackageVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long packageId) {
        return R.ok(tenantPackageService.queryById(packageId));
    }

    /**
     * 新增租户套餐
     */
    @PreAuthorize(hasAnyPermi = "system:tenantPackage:add",andRoles = TenantConstants.SUPER_ADMIN_ROLE_KEY)
    @OperatorLog(title = "租户套餐", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysTenantPackageBo bo) {
        return R.toAjax(tenantPackageService.insertByBo(bo));
    }

    /**
     * 修改租户套餐
     */
    @PreAuthorize(hasAnyPermi = "system:tenantPackage:edit",andRoles = TenantConstants.SUPER_ADMIN_ROLE_KEY)
    @OperatorLog(title = "租户套餐", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysTenantPackageBo bo) {
        return R.toAjax(tenantPackageService.updateByBo(bo));
    }

    /**
     * 状态修改
     */
    @PreAuthorize(hasAnyPermi = "system:tenantPackage:edit",andRoles = TenantConstants.SUPER_ADMIN_ROLE_KEY)
    @OperatorLog(title = "租户套餐", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public R<Void> changeStatus(@RequestBody SysTenantPackageBo bo) {
        return toAjax(tenantPackageService.updatePackageStatus(bo));
    }

    /**
     * 删除租户套餐
     *
     * @param packageIds 主键串
     */
    @PreAuthorize(hasAnyPermi = "system:tenantPackage:remove",andRoles = TenantConstants.SUPER_ADMIN_ROLE_KEY)
    @OperatorLog(title = "租户套餐", businessType = BusinessType.DELETE)
    @DeleteMapping("/{packageIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] packageIds) {
        return toAjax(tenantPackageService.deleteWithValidByIds(Arrays.asList(packageIds), true));
    }
}
